package com.enterprise.web;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.assg2.dao.*;
import com.movie.Actor;
import com.movie.Movie;
import com.movie.Review;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

/**
 * This is the command that will be used for logging in users.
 * If logon is successful, the command should place a list of contacts
 * in the request attriubutes.
 * @author  yunki
 */
public class InfoCommand implements Command {

    /** Creates a new instance of LoginCommand */
    public InfoCommand() {
    }

    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //TODO: set the response content type to "text/html" here
        //TODO: You'll have to use getWriter() to output some HTML texts
        //TODO: close the output stream

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            int movieid = Integer.parseInt(request.getParameter("movieid"));
            Movie movie = new Movie();
            DaoMovie dm = new DaoMovie();
            movie = (Movie) dm.findById(movieid);
            Set<Review> reviewSet = movie.getReviews();
            Set<Actor> actorSet = movie.getActors();
            ArrayList nicknameList = new ArrayList();
            ArrayList commentList = new ArrayList();
            ArrayList dateList = new ArrayList();
            ArrayList actorList = new ArrayList();
            Iterator i = actorSet.iterator();
            while (i.hasNext()) {
                Actor actor = null;
                actor = (Actor) i.next();
                String name = actor.getFirstname() + " " + actor.getLastname();
                actorList.add(name);
            }


            Iterator it = reviewSet.iterator();
            // sort by date
            ArrayList<Review> sortList = new ArrayList();

            while (it.hasNext()) {
                Review review = (Review) it.next();
                sortList.add(review);
            }

            for (int j = 0; j < sortList.size(); j++) {
                Review temp = null;
                for (int g = sortList.size() - 1; g > j; g--) {
                    if (sortList.get(g).getDateposted().before(sortList.get(g - 1).getDateposted())) {
                        temp = sortList.get(g);
                        sortList.set(g, sortList.get(g - 1));
                        sortList.set(g - 1, temp);
                    }
                }
            }

            for (int h = 0; h < sortList.size(); h++) {
                Review r = (Review) sortList.get(h);
                String nickname = r.getViewer().getNickname();
                String comment = r.getDescription();
                Date date = r.getDateposted();
                nicknameList.add(nickname);
                commentList.add(comment);
                dateList.add(date);

            }
            request.setAttribute("resultmovie", movie);
            request.setAttribute("nick", nicknameList);
            request.setAttribute("comment", commentList);
            request.setAttribute("date", dateList);
            request.setAttribute("actor", actorList);
            
        } finally {
           return "/movie.jsp";
        }
    }
}
